#if 1
#include "Uart.h"
#include "stm32h7xx_hal.h"

#define RxBufSize

UART_HandleTypeDef huart1 = {0};
uint8_t RxBuf[2][RxBufSize] __attribute__((section(".RAM_D1")));
void (*Uart1RxCompleteCallback)(uint8_t *pData, uint16_t *Count);

void Uart1Init(uint32_t BaudRate, void (*RxCompleteCallback)(uint8_t *pData, uint16_t *Count))
{
	huart1.Instance = USART1;
	huart1.Init.BaudRate = BaudRate;
	huart1.Init.ClockPrescaler = UART_PRESCALER_DIV2;
	huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
	huart1.Init.Mode = UART_MODE_TX_RX;
	huart1.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED;
	huart1.Init.OverSampling = UART_OVERSAMPLING_8;
	huart1.Init.Parity = UART_PARITY_NONE;
	huart1.Init.StopBits = UART_STOPBITS_1;
	huart1.Init.WordLength = UART_WORDLENGTH_8B;
	huart1.FifoMode = UART_FIFOMODE_DISABLE;

	HAL_UART_Init(&huart1);
	HAL_UART_Receive_IT(&huart1, RxBuf[0], RxBufSize);
	__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
	HAL_NVIC_EnableIRQ(USART1_IRQn);
	HAL_NVIC_SetPriority(USART1_IRQn, 11, 0);

	Uart1RxCompleteCallback = RxCompleteCallback;
}

void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
	if (huart == &huart1) // 串口1
	{
		GPIO_InitTypeDef GPIO_InitStruct;

		GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
		GPIO_InitStruct.Pull = GPIO_NOPULL;
		GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
		GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
		GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_9;

		__HAL_RCC_GPIOA_CLK_ENABLE();
		__HAL_RCC_USART1_CLK_ENABLE();

		HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
	}
}

void Uart1TxData(uint8_t *pData, uint16_t Count)
{
	HAL_UART_Transmit_IT(&huart1, pData, Count);
}

void USART1_IRQHandler(void)
{
	HAL_UART_IRQHandler(&huart1);
	if (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE))
	{
		static uint16_t count;
		__HAL_UART_CLEAR_IDLEFLAG(&huart1);
		if (Uart1RxCompleteCallback)
		{
			count = RxBufSize - huart1.RxXferCount;
			huart1.RxState = HAL_UART_STATE_READY;
			if (huart1.pRxBuffPtr <= RxBuf[1])
				Uart1RxCompleteCallback(RxBuf[0], &count);
			else
				Uart1RxCompleteCallback(RxBuf[1], &count);
			HAL_UART_RxCpltCallback(&huart1);
		}
	}
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if (huart == &huart1)
	{
		if (huart->pRxBuffPtr <= RxBuf[1])
			HAL_UART_Receive_IT(&huart1, RxBuf[1], RxBufSize);
		else
			HAL_UART_Receive_IT(&huart1, RxBuf[0], RxBufSize);
	}
}

#ifndef __Uart_H_
#define __Uart_H_

#include "stdint.h"

void Uart1Init(uint32_t BaudRate, void (*RxCompleteCallback)(uint8_t *pData, uint16_t *Count));
void Uart1TxData(uint8_t *pData, uint16_t Count);

#endif /* End __Uart_H_ */ /
就是这么简单明了，咦，是不是发现接口函数只有发送没有接收，没关系，咱继续，贴一个简单应用，代码如下：

#include "Uart1Task.h"
#include "limits.h" //ULONG_MAX
#include "Uart.h"' 

#define Uart1RxCompleteFlag 0x01

static uint8_t *Uart1RxData;
static uint16_t Uart1RxCount;
TaskHandle_t Uart1TaskHandle;

void Uart1RxIRQ(uint8_t *pData, uint16_t *Count);

TaskHandle_t *GetTaskHandle_t(void)
{
	return &Uart1TaskHandle;
}

void Uart1Task(void *pvParameter)
{
	Uart1Init(115200, Uart1RxIRQ);
	while (1)
	{
		uint32_t NotifyValue = 0;
		xTaskNotifyWait(pdFALSE, ULONG_MAX, &NotifyValue, portMAX_DELAY);
		Uart1TxData(Uart1RxData, Uart1RxCount);
	}
}

void Uart1RxIRQ(uint8_t *pData, uint16_t *Count)
{
	Uart1RxData = pData;
	Uart1RxCount = *Count;
	BaseType_t pxHigherPriorityTaskWoken;
	xTaskNotifyFromISR(*GetTaskHandle_t(), Uart1RxCompleteFlag, eSetBits, &pxHigherPriorityTaskWoken);
	portYIELD_FROM_ISR(pxHigherPriorityTaskWoken);
}
#endif

void UART7_IRQHandler(void)
{
	char ch;
	if ((__HAL_UART_GET_FLAG(&huart7, UART_FLAG_RXNE) != RESET) &&
		(__HAL_UART_GET_IT_SOURCE(&huart7, UART_IT_RXNE) != RESET))
	{
		ch = huart7.Instance->RDR;
		__HAL_TIM_SET_COUNTER(&htim6, 0);
		if (USART7_RX_STA & 0x8000)
			return;				// 上次接收完成的未处理，直接退出
		if (USART7_RX_STA == 0) // 长度为0，接收到的是第一个字节，启动定时器
		{
			__HAL_TIM_CLEAR_FLAG(&htim6, TIM_FLAG_UPDATE);
			HAL_TIM_Base_Start_IT(&htim6);
		}
		USART7_RX_BUF[USART7_RX_STA & 0x3FFF] = ch;
		USART7_RX_STA++;
		if (USART7_RX_STA > (USART_REC_LEN - 1))
			USART7_RX_STA = 0;
	}
	else
	{
		if (__HAL_UART_GET_FLAG(&huart7, UART_FLAG_ORE) != RESET)
		{
			__HAL_UART_CLEAR_OREFLAG(&huart7);
		}
		if (__HAL_UART_GET_FLAG(&huart7, UART_FLAG_NE) != RESET)
		{
			__HAL_UART_CLEAR_NEFLAG(&huart7);
		}
		if (__HAL_UART_GET_FLAG(&huart7, UART_FLAG_FE) != RESET)
		{
			__HAL_UART_CLEAR_FEFLAG(&huart7);
		}
		if (__HAL_UART_GET_FLAG(&huart7, UART_FLAG_PE) != RESET)
		{
			__HAL_UART_CLEAR_PEFLAG(&huart7);
		}
		if (__HAL_UART_GET_FLAG(&huart7, UART_FLAG_CTS) != RESET)
		{
			__HAL_UART_CLEAR_IT(&huart7, UART_FLAG_CTS);
		}
		if (__HAL_UART_GET_FLAG(&huart7, UART_FLAG_TXE) != RESET)
		{
			__HAL_UART_CLEAR_IT(&huart7, UART_FLAG_TXE);
		}
		if (__HAL_UART_GET_FLAG(&huart7, UART_FLAG_TC) != RESET)
		{
			__HAL_UART_CLEAR_IT(&huart7, UART_FLAG_TC);
		}
		if (__HAL_UART_GET_FLAG(&huart7, UART_FLAG_RXNE) != RESET)
		{
			__HAL_UART_CLEAR_IT(&huart7, UART_FLAG_RXNE);
		}
	}
}

void UART7_IRQHandler(void)
{
	char ch;
	if ((__HAL_UART_GET_FLAG(&huart7, UART_FLAG_RXNE) != RESET) &&
		(__HAL_UART_GET_IT_SOURCE(&huart7, UART_IT_RXNE) != RESET))
	{
		ch = huart7.Instance->RDR;
		__HAL_TIM_SET_COUNTER(&htim6, 0);
		if (USART7_RX_STA & 0x8000)
			return;				// 上次接收完成的未处理，直接退出7 o( _' T' ]! z" ?+ E' p. Q
		if (USART7_RX_STA == 0) // 长度为0，接收到的是第一个字节，启动定时器
		{
			__HAL_TIM_CLEAR_FLAG(&htim6, TIM_FLAG_UPDATE);
			HAL_TIM_Base_Start_IT(&htim6);
		}
		USART7_RX_BUF[USART7_RX_STA & 0x3FFF] = ch;
		USART7_RX_STA++;
		if (USART7_RX_STA > (USART_REC_LEN - 1))
			USART7_RX_STA = 0;
	}
	else
	{
		if (__HAL_UART_GET_FLAG(&huart7, UART_FLAG_ORE) != RESET)
		{
			__HAL_UART_CLEAR_OREFLAG(&huart7);
		}
		if (__HAL_UART_GET_FLAG(&huart7, UART_FLAG_NE) != RESET)
		{
			__HAL_UART_CLEAR_NEFLAG(&huart7);
		}
		if (__HAL_UART_GET_FLAG(&huart7, UART_FLAG_FE) != RESET)
		{
			__HAL_UART_CLEAR_FEFLAG(&huart7);
		}
		if (__HAL_UART_GET_FLAG(&huart7, UART_FLAG_PE) != RESET)
		{
			__HAL_UART_CLEAR_PEFLAG(&huart7);
		}
		if (__HAL_UART_GET_FLAG(&huart7, UART_FLAG_CTS) != RESET)
		{
			__HAL_UART_CLEAR_IT(&huart7, UART_FLAG_CTS);
		}
		if (__HAL_UART_GET_FLAG(&huart7, UART_FLAG_TXE) != RESET)
		{
			__HAL_UART_CLEAR_IT(&huart7, UART_FLAG_TXE);
		}
		if (__HAL_UART_GET_FLAG(&huart7, UART_FLAG_TC) != RESET)
		{
			__HAL_UART_CLEAR_IT(&huart7, UART_FLAG_TC);
		}
		if (__HAL_UART_GET_FLAG(&huart7, UART_FLAG_RXNE) != RESET)
		{
			__HAL_UART_CLEAR_IT(&huart7, UART_FLAG_RXNE);
		}
	}
}






static void uart isr(UART HandleTypeDef *UartHandle,uint8 t type)
{
	uint32 t isrflags- READ REG(UartHandle->Instance->R);
	uint32 t criits= READ REG(UartHandle->Instance->CR1);
	uint8 t rec;
	UartHandle->pRxBuffptr = &gUartValue[type];

	if(((isrflagS & USART_SR RXNE)!- RESET)&&((cr1its & USART_CR1 RXNEIE) != RESET))
	{
		rec=(uint8 t)(UartHandle->Instance->DR &(uint8 t)@x80FF);;
		rt_hw_serial_isr(UartHandle,rec);			//新的接收函数,也可以调用hal的接收回调函数.
		__HAL_UART_CLEAR_FLAG(UartHandle, UART_FLAG_RXNE);		////关键!!!!这里启动了下一次务
	}
	/* UART in mode ransmitter */
	if(((isrflagS & USART SR TXE)!=RESET)8&((cr1its & USART_CR1 TXEIE !=RESET)))
	{
		/* Check that a Tx process is ongoing */
		if(UartHandle->gState == HAL_UART_STATE_BUSY_TX)
		{
			/* Check if the TxFIFO is empty */
			if(UartHandle->Instance->SR & USART_SR_TXE)
			{
				uint16 t *tmp;
				if (UartHandle->Init.WordLength == UART NORDLENGTH_9B)
				{
					tmp =(uint16 t*)UartHandle >pTxBuffptr;
					UartHandle->Instance->DR =(uint16 t)(*tmp & (uint16 t)0xe1FF);
					if (UartHandle->Init,Parity = UART_PARITY_NONE)
					{
						UartHandle->pTxBuffPtr += 2U;
					}
					else
					{
						UartHandle->pTxBuffptr += 1U;
					}
				}
				else
				{
					UartHandle->Instance->DR =(uint8 t)( *UartHandle->pTxBuffPtr++ &(u
				}

				if(--UartHandle->TxXferCount == U)
				{
					/*Disable the UART Transmit complete Interrupt */
					HAL_UART_DISABLE_IT(UartHandle, UART_IT_TXE);
					/*Enable the UART Transmit Complete Interrupt */
					HAL_UART_ENABLE_IT(UartHandle, UART_IT_TC);
				}
			}
		}

		/*UART in mode Transmitter end--------*/
		if(((iSrflagS & USART SR TC)!= RESET)&&((cr1itS & USART CR1 TCIE)!- RESET))
		{
			/*Disable the UART Iransmit complete Interrupt*/
			HAL_UART_DISABLE_IT(UartHandle, UART_IT_TC);
			/*Tx process is ended, restore huart->gstate to Ready */
			UartHandle->gState = HAL UART STATE READY;
			/*这里可以添加发送完成中断回调函数*/
		}
	}

}






25
26
18
29
30
31
uint16 t *tmp;
if (UartHandle->Init.WordLength == UART NORDLENGTH_9B)
if (UartHandle->Init,Parity = UART_PARITY_NONE)
觉得还不错?-键收藏
UartHandle->pTxBuffPtr
mo 47132384关注
图片童看
髕混RustDesk
1155962257@welburder
每21会 74
